<template>
  <div class="app-container">
    <el-row :gutter="20" v-loading="loading">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <!--          <el-input-->
          <!--            placeholder="请输入部门名称"-->
          <!--            clearable-->
          <!--            size="small"-->
          <!--            prefix-icon="el-icon-search"-->
          <!--            style="margin-bottom: 20px"-->
          <!--          />-->
        </div>
        <div
          class="head-container"
          style="margin-top: 20px; overflow: auto; height: 76vh"
        >
          <el-tree
            :data="configOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="物资名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入物资名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物资编码" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入物资编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['materials:productInfo:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['materials:productInfo:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['materials:productInfo:remove']"
              >删除</el-button
            >
          </el-col>
          <!--          <el-col :span="1.5">-->
          <!--            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['materials:productInfo:export']">导出</el-button>-->
          <!--          </el-col>-->
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExportQr"
              v-hasPermi="['materials:productInfo:exportQr']"
              >二维码下载</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :disabled="multiple"
              @click="handleDownload"
              v-hasPermi="['materials:productInfo:exportQr']"
              >选中二维码下载</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              :disabled="refreshAll"
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="allRefreshQr"
              v-hasPermi="['materials:productInfo:allRefreshQr']"
              >刷新二维码</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          :data="productInfoList"
          @selection-change="handleSelectionChange"
          max-height="550"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!--          <el-table-column label="编号" align="center" prop="id" />-->
          <el-table-column
            label="物资名称"
            align="center"
            prop="name"
            minWidth="200px"
          />
          <el-table-column label="物资编码" align="center" prop="code" />
          <el-table-column
            label="规格型号"
            align="center"
            prop="model"
            minWidth="140px"
          />
          <el-table-column label="单位" align="center" prop="unit" />
          <!--          <el-table-column label="价格" align="center" prop="price" />-->
          <el-table-column label="二维码" align="center" prop="qrUrl">
            <template slot-scope="scope">
              <img :src="scope.row.picUrl" style="width: 50px; height: 50px" />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            min-width="200"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                >查看</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['materials:productInfo:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['materials:productInfo:remove']"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-refresh"
                @click="newQrCode(scope.row)"
                v-hasPermi="['materials:productInfo:refreshQr']"
                >刷新二维码</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改物资信息管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      v-myDialogDrag
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物资名称" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="请输入物资名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物资编码" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入物资编码"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物资类别" prop="typeCode">
              <treeselect
                v-model="form.typeCode"
                :options="configOptions"
                :normalizer="normalizer"
                placeholder="请选择上级节点"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="model">
              <el-input
                v-model="form.model"
                placeholder="请输入规格型号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input
                v-model="form.unit"
                placeholder="请输入单位"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <!--              <el-col :span="12">-->
          <!--                <el-form-item label="价格" prop="price">-->
          <!--                  <el-input v-model="form.price" placeholder="请输入价格" :disabled="openDisabled" />-->
          <!--                </el-form-item>-->
          <!--              </el-col>-->
          <!--              <el-col :span="12">-->
          <!--                <el-form-item label="状态">-->
          <!--                  <el-radio-group v-model="form.status" :disabled="openDisabled">-->
          <!--                    <el-radio label="1">请选择字典生成</el-radio>-->
          <!--                  </el-radio-group>-->
          <!--                </el-form-item>-->
          <!--              </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码信息" prop="picUrl">
              <!--                  <el-input v-model="form.picUrl" placeholder="请输入二维码信息" :disabled="openDisabled" />-->
              <el-image :src="form.picUrl"></el-image>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductInfo,
  getProductInfo,
  delProductInfo,
  addProductInfo,
  updateProductInfo,
  newQrCode,
  allRefreshQr,
} from "@/api/equipment/materials/productInfo";
import { treeselect } from "@/api/equipment/materials/productConfig";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { downLoadZip, downLoadZip2, resolveBlob2 } from "@/utils/zipdownload";
export default {
  name: "ProductInfo",
  components: {
    Treeselect,
  },
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      refreshAll: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资信息管理表格数据
      productInfoList: [],
      configOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 包装类型字典
      packTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        id: null,
        name: null,
        code: null,
        typeCode: null,
        type: null,
        isStatistics: null,
        stockNum: null,
        ids: null,
      },
      defaultProps: {
        children: "children",
        label: "typeName",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "不能为空", trigger: ["blur", "change"] },
        ],
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        /*        name: [{  max: 8, message: "不能为空且长度不能超过8个字", trigger: "change" },
            { required: true, message: "不能为空", trigger: "blur" }],*/
        typeCode: [{ required: true, message: "不能为空", trigger: "blur" }],
        unit: [{ required: true, message: "不能为空", trigger: "blur" }],
        price: [{ required: true, message: "不能为空", trigger: "blur" }],
        /*        model: [{ required: true,max: 8,  message: "不能为空且长度不能超过8个字", trigger: "blur" }],*/
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("package_type").then((response) => {
      this.packTypeOptions = response.data;
    });
  },
  watch: {
    // 监听deptId
    "queryParams.code": "handleQuery",
    "queryParams.name": "handleQuery",
  },
  methods: {
    newQrCode(row) {
      const id = row.id;
      newQrCode(id).then((response) => {
        this.msgSuccess("刷新二维码成功。");
        this.getList();
      });
    },
    allRefreshQr() {
      this.loading = true;
      allRefreshQr().then((response) => {
        this.msgSuccess("刷新二维码成功。");
        this.getList();
        this.loading = false;
      });
    },
    handleExportQr() {
      this.download(
        "equipment/eqp/productInfo/download",
        {
          ...this.queryParams,
        },
        "生产物资二维码" + this.parseTime(new Date()) + ".zip"
      );
    },
    handleDownload(row) {
      const ids = row.id || this.ids;
      /*downloadProductInfo(ids).then(response =>{
                  resolveBlob2(response,'物料二维码');
                });*/
      // this.downLoadZip2("equipment/eqp/productInfo/download", ids, '生产物资二维码'+this.parseTime(new Date)+'.zip');
      this.queryParams.ids = ids;
      this.download(
        "equipment/eqp/productInfo/downloadByIds",
        {
          ...this.queryParams,
        },
        "货位二维码" + this.parseTime(new Date()) + ".zip"
      );
    },
    /** 查询物资信息管理列表 */
    getList() {
      this.loading = true;
      listProductInfo(this.queryParams).then((response) => {
        this.productInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 包装类型字典翻译
    packTypeFormat(row, column) {
      return this.selectDictLabel(this.packTypeOptions, row.packType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        typeCode: null,
        type: null,
        model: null,
        unit: null,
        picUrl: null,
        orderNum: null,
        bottle: null,
        factoryCode: null,
        packType: null,
        number: null,
        capacity: null,
        isStandard: null,
        alarmNum: null,
        isStatistics: null,
        price: null,
        source: null,
        status: "0",
        delFlag: null,
        stockNum: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加物资信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getProductInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物资信息管理";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getProductInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看物资信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProductInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addProductInfo(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除物资信息管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delProductInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equipment/eqp/productInfo/export",
        {
          ...this.queryParams,
        },
        "生产物资信息" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.configOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.id = data.id;
      this.getList();
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.typeCode,
        label: node.typeName,
        children: node.children,
      };
    },
  },
};
</script>
